'use strict';
angular.module('app.directives', []).directive("myCheckboxGroup", function ($compile, _) {
    return {
        transclude: true,
        scope: {
            destarray: "=",
            fieldobj: "=",
            validationfunc: "&"
        },
        link: function ($scope, elem, attrs, ngModelController) {
            if($scope.destarray !== undefined && $scope.fieldobj !== undefined) {
                var rec3 = _.findWhere($scope.destarray, {
                    hazardTypeID: $scope.fieldobj
                });
                if(rec3 !== undefined) {
                    elem[0].checked = true;
                    $scope.$apply();
                }
            }
            elem.bind('change', function () {
                var rec = _.findWhere($scope.destarray, {
                    hazardTypeID: $scope.fieldobj
                });
                if(elem[0].checked) {
                    if(rec == undefined) {
                        if($scope.destarray.length > 0) {
                            var rec = $scope.destarray[0];
                            $scope.destarray.push({
                                id: 0,
                                locationID: rec.locationID,
                                location: rec.location,
                                hazardTypeID: $scope.fieldobj
                            });
                            $scope.$parent.$parent.location.locationHazards.push({
                                id: 0,
                                locationID: rec.locationID,
                                location: rec.location,
                                hazardTypeID: $scope.fieldobj,
                                lookup: null
                            });
                            $scope.$apply();
                            $scope.$parent.$parent.form.$setDirty();
                        } else {
                            $scope.destarray.push({
                            });
                            $scope.destarray[0].hazardTypeID = $scope.fieldobj;
                            $scope.destarray[0].location = $scope.$parent.$parent.location;
                            $scope.destarray[0].locationID = $scope.$parent.$parent.location.id;
                            var rec = $scope.destarray[0];
                            $scope.$parent.$parent.location.locationHazards.push(rec);
                            $scope.$apply();
                            $scope.$parent.$parent.form.$setDirty();
                        }
                    }
                } else {
                    if(rec !== undefined) {
                        var log;
                        $scope.destarray.splice(rec.id, 1);
                        $scope.$parent.$parent.location.locationHazards.forEach(function (element, index, array) {
                            if(element.id == rec.id) {
                                $scope.$parent.$parent.location.locationHazards.splice(index, 1);
                            }
                        });
                        $scope.$apply();
                    }
                }
            });
        }
    };
}).directive("loadingWidget", [
    '_START_REQUEST_', 
    '_END_REQUEST_', 
    function (_START_REQUEST_, _END_REQUEST_) {
        return {
            restrict: "A",
            link: function (scope, element) {
                element.hide();
                scope.$on(_START_REQUEST_, function () {
                    element.show();
                });
                scope.$on(_END_REQUEST_, function () {
                    element.hide();
                });
            }
        };
    }]);
